package de.hhbk.feedbackmodul.beans.sessionscope;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.model.SelectItem;

import de.hhbk.feedbackmodul.beans.applicationscope.ApplicationManagerBean;
import de.hhbk.feedbackmodul.entities.AnswerPossibility;
import de.hhbk.feedbackmodul.entities.Participant;
import de.hhbk.feedbackmodul.entities.Question;
import de.hhbk.feedbackmodul.entities.QuestionSheet;
import de.hhbk.feedbackmodul.entities.Survey;
import de.hhbk.feedbackmodul.entities.Tan;
import de.hhbk.feedbackmodul.entities.User;
import de.hhbk.feedbackmodul.entities.UserRole;
import de.hhbk.feedbackmodul.enums.FragenTyp;
import de.hhbk.feedbackmodul.enums.MessageLabel;
import de.hhbk.feedbackmodul.enums.MessageStyle;
import de.hhbk.feedbackmodul.util.UserInfo;

public class SessionManagerBean{

	private ApplicationManagerBean masterBean;
	
	private UserInfo userinfo;
	
	private List<String> messages;
	
	private MessageStyle messageStyle;
	
	private MessageLabel messageLabel;
	
	@PostConstruct
	public void init()
	{
		messages = new ArrayList<String>();
		messageStyle = MessageStyle.info;
		messageLabel = MessageLabel.info;
	}
	
	/* MESSAGE SYSTEM */
	
	public void addMessageParams(String addMessage,MessageStyle style,MessageLabel label)
	{
		messages.add(addMessage);
		messageStyle = style;
		messageLabel = label;
	}
	
	public void clearMessages()
	{
		messages.removeAll(messages);
	}
	
	public boolean isMessageAvailable()
	{
		if(messages.size() > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/* CACHE FINDER METHODEN */
	
	public UserRole findUserRoleByID(int id)
	{
		List<UserRole> list = masterBean.getUserRoleList();
		for(UserRole br : list)
		{
			if(br.getId() == id)
			{
				return br;
			}
		}
		return null;
	}
	
	public User findUserByID(String id)
	{
		List<User> list = masterBean.getUserlist();
		for(User u : list)
		{
			if(u.getId().equalsIgnoreCase(id))
			{
				return u;
			}
		}
		return null;
	}
	
	public Participant findParticipantByName(String name,int id)
	{
		List<Participant> list = masterBean.getParticipantList();
		for(Participant p : list)
		{
			if(p.getName().equalsIgnoreCase(name) && p.getId() != id)
			{
				return p;
			}
		}
		return null;
	}
	
	public Survey findSurveyByName(String name,int id)
	{
		List<Survey> list = masterBean.getSurveyList();
		for(Survey s : list)
		{
			if(s.getBezeichnung().equalsIgnoreCase(name) && s.isStatus() && s.getId() != id)
			{
				return s;
			}
		}
		return null;
	}

	public Survey findSurveyById(int id)
	{
		List<Survey> list = masterBean.getSurveyList();
		for(Survey s : list)
		{
			if(s.getId() == id)
			{
				return s;
			}
		}
		return null;
	}
	
	public Tan findTanByID(String id)
	{
		List<Tan> list = masterBean.getTanList();
		for(Tan t: list)
		{
			if(id.equals(String.valueOf(t.getTan_nummer())))
			{
				return t;
			}
		}
		return null;
	}
	
	/* CACHE LISTEN */
	
	public List<AnswerPossibility> getAllAnswerPossibilities()
	{
		return new ArrayList<AnswerPossibility>(masterBean.getAnswerPossibilityList());
	}
	
	public List<Question> getAllQuestions()
	{
		return new ArrayList<Question>(masterBean.getQuestionList());
	}
	
	public List<QuestionSheet> getAllQuestionSheets()
	{
		return new ArrayList<QuestionSheet>(masterBean.getQuestionSheetList());
	}
	
	public List<SelectItem> getUserIDList()
	{
		List<SelectItem> returnList = new ArrayList<SelectItem>();
		List<User> userList = masterBean.getUserlist();
		for(User u : userList)
		{
			returnList.add(new SelectItem(u.getId()));
		}
		return returnList;
	}
	
	public List<String> getUserIDListAsStrings()
	{
		List<String> returnList = new ArrayList<String>();
		List<User> userList = masterBean.getUserlist();
		for(User u : userList)
		{
			returnList.add(u.getId());
		}
		return returnList;
	}
	
	public List<String> getQuestionSheetLabelListAsStrings()
	{
		List<String> returnList = new ArrayList<String>();
		List<QuestionSheet> questionSheetList = masterBean.getQuestionSheetList();
		for(QuestionSheet qs : questionSheetList)
		{
			returnList.add(qs.getBezeichnung());
		}
		return returnList;
	}
	
	public List<String> getSurveyLabelListAsStrings()
	{
		List<String> returnList = new ArrayList<String>();
		List<Survey> surveyList = masterBean.getSurveyList();
		for(Survey s : surveyList)
		{
			returnList.add(s.getBezeichnung());
		}
		return returnList;
	}
	
	public List<String> getParticipantNameListAsStrings()
	{
		List<String> returnList = new ArrayList<String>();
		List<Participant> participantList = masterBean.getParticipantList();
		for(Participant p : participantList)
		{
			returnList.add(p.getName());
		}
		return returnList;
	}
	
	public List<String> getQuestionListAsStrings()
	{
		List<String> returnList = new ArrayList<String>();
		List<Question> questionList = masterBean.getQuestionList();
		for(Question q : questionList)
		{
			returnList.add(q.getText());
		}
		return returnList;
	}
	
	public List<SelectItem> getQuestionTypeList()
	{
		List<SelectItem> returnList = new ArrayList<SelectItem>();
		returnList.add(new SelectItem(FragenTyp.normal.toString(), FragenTyp.normal.toString()));
		returnList.add(new SelectItem(FragenTyp.multiple_choice.toString(), FragenTyp.multiple_choice.toString()));
		return returnList;
	}
	
	public List<SelectItem> getQuestionIDList()
	{
		List<SelectItem> returnList = new ArrayList<SelectItem>();
		List<Question> questionList = masterBean.getQuestionList();
		for(Question q : questionList)
		{
			returnList.add(new SelectItem(q.getId()+""));
		}
		return returnList;
	}
	
	/* GETTER + SETTER */
	
	public ApplicationManagerBean getMasterBean() {
		return masterBean;
	}

	public void setMasterBean(ApplicationManagerBean masterBean) {
		this.masterBean = masterBean;
	}

	public UserInfo getUserinfo() {
		return userinfo;
	}

	public void setUserinfo(UserInfo userinfo) {
		this.userinfo = userinfo;
	}

	public List<String> getMessages() {
		return messages;
	}

	public void setMessages(List<String> messages) {
		this.messages = messages;
	}

	public MessageStyle getMessageStyle() {
		return messageStyle;
	}

	public void setMessageStyle(MessageStyle messageStyle) {
		this.messageStyle = messageStyle;
	}

	public MessageLabel getMessageLabel() {
		return messageLabel;
	}

	public void setMessageLabel(MessageLabel messageLabel) {
		this.messageLabel = messageLabel;
	}

}
